import type {
  LoadingConfig,
  AdaptiveConfig,
  PaginationProps
} from "@pureadmin/table";
import { ref, onMounted, reactive } from "vue";
import { isAllEmpty } from "@pureadmin/utils";
import { getOrders } from "@/api/mall";

export function useColumns() {
  const dataList = ref([]);
  const loading = ref(true);
  const columns: TableColumnList = [
    {
      label: "订单号",
      prop: "no"
    },
    {
      label: "用户ID",
      prop: "member_id"
    },

    {
      label: "发货地址",
      prop: "address"
    },
    {
      label: "等级",
      width: 45,
      prop: "userLevel"
    },
    {
      label: "状态",
      prop: "status",
      formatter: row => {
        switch (row.status) {
          case 1:
            return "待付款";
          case 2:
            return "待发货";
          case 3:
            return "待收货";
          case 4:
            return "已取消";
          case 5:
            return "已完成";
          case 7:
            return "申请退款";
          case 8:
            return "待填写地址";
          default:
            return "未知";
        }
      }
    },
    {
      label: "下单时间",
      prop: "created_at"
    },
    {
      label: "操作",
      slot: "operation",
      width: 140,
      fixed: "right"
    }
  ];

  /** 分页配置 */
  const pagination = reactive<PaginationProps>({
    pageSize: 10,
    currentPage: 1,
    pageSizes: [10, 15, 20],
    total: 0,
    align: "right",
    background: true,
    size: "default"
  });

  // const keyWord = ref("");
  // const startTime = ref("");
  // const no = ref("");
  // const orderStatus = ref(0);

  /** 加载动画配置 */
  const loadingConfig = reactive<LoadingConfig>({
    text: "正在加载第一页...",
    viewBox: "-10, -10, 50, 50",
    spinner: `
        <path class="path" d="
          M 30 15
          L 28 17
          M 25.61 25.61
          A 15 15, 0, 0, 1, 15 30
          A 15 15, 0, 1, 1, 27.99 7.5
          L 15 15
        " style="stroke-width: 4px; fill: rgba(0, 0, 0, 0)"/>
      `
    // svg: "",
    // background: rgba()
  });

  /** 撑满内容区自适应高度相关配置 */
  const adaptiveConfig: AdaptiveConfig = {
    /** 表格距离页面底部的偏移量，默认值为 `96` */
    offsetBottom: 110
    /** 是否固定表头，默认值为 `true`（如果不想固定表头，fixHeader设置为false并且表格要设置table-layout="auto"） */
    // fixHeader: true
    /** 页面 `resize` 时的防抖时间，默认值为 `60` ms */
    // timeout: 60
    /** 表头的 `z-index`，默认值为 `100` */
    // zIndex: 100
  };

  function onSizeChange(val) {
    console.log("onSizeChange", val);
    dataList.value = [];
    pagination.currentPage = 1;
    loadData();
  }

  const params = ref({});
  function onKeywordChange(val) {
    params.value = val;
    // keyWord.value = keyWord_;
    // startTime.value = startTime_;
    // no.value = no_;
    // orderStatus.value = orderStatus_;
    dataList.value = [];
    pagination.currentPage = 1;
    loadData();
  }

  function onCurrentChange(val) {
    loadingConfig.text = `正在加载第${val}页...`;
    console.log("onCurrentChange", val, pagination.currentPage);
    loading.value = true;

    loadData().then(() => {
      loading.value = false;
    });
  }

  function loadData() {
    return new Promise((resolve, reject) => {
      // const params = {
      //   page: pagination.currentPage,
      //   pageSize: pagination.pageSize,
      //   starrating: 0
      // };
      // if (!isAllEmpty(keyWord.value)) {
      //   params["keyWord"] = keyWord.value;
      // }
      // if (!isAllEmpty(orderStatus.value) && orderStatus.value !== 0) {
      //   params["orderStatus"] = orderStatus.value;
      // }
      // if (!isAllEmpty(no.value)) {
      //   params["no"] = no.value;
      // }
      // if (!isAllEmpty(startTime.value)) {
      //   params["startTime"] = startTime.value;
      // }
      params.value.page = pagination.currentPage;
      params.value.pageSize = pagination.pageSize;
      getOrders(params.value)
        .then(res => {
          const data = res.data;
          dataList.value = [...data.items];
          pagination.total = data.count;
          loading.value = false;
          resolve(res.data);
        })
        .catch(error => {
          reject(error);
        });
    });
  }

  onMounted(() => {
    loadData();
  });

  return {
    loading,
    columns,
    dataList,
    pagination,
    loadingConfig,
    adaptiveConfig,
    onSizeChange,
    loadData,
    onKeywordChange,
    onCurrentChange
  };
}
